package com.none.web.controller.comments;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;











import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TComments;
import com.none.web.service.comments.ICommentsService;

/**
 * 评论管理Controller
 * 
 * @author Summer
 * 
 */
@Controller
@RequestMapping("comment")
public class CommentsController {

	@Resource(name = "commentService")
	ICommentsService commentService;

	@Autowired
	private HttpServletRequest request;
	/**
	 * 添加评论
	 * 
	 * @param comment
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveComment")
	public Object saveComments(@FormModel("comment") final TComments comment, String type,String userId) throws Exception {
		String[] filter = new String[] {};
		
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}

		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);

		data.put("commentObj", commentService.saveComments(comment, type, userId));
		return JSONUtil.toJSON(result, filter);
	}
	/**
	 * 删除评论
	 * 
	 * @param commentId
	 * @param flag
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("deleteComments")
	public Object deleteVideoComments(String commentId,String flag,String userId) throws Exception {

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		
		Map<String, Object> data = new HashMap<String, Object>();
		boolean bool=commentService.deleteComments(commentId, userId,flag);
		data.put("result", bool);
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);
		if (!bool) {
			result = new Result(SysConstant.STATE_ERROR, "golbal.error", data);
		}
		return JSONUtil.toJSON(result);
	}
	
	
	/**
	 * APP查询所有评论（不包含Block的，即block_state=1的）
	 * 
	 * @param pager
	 * @param contentId
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getCommentsList")
	public Object getCommentsById(@FormModel("pager") final Pager pager, String contentId,String type,String lastTime,String userId) throws Exception {

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		
		Map<String, Object> data = new HashMap<String, Object>();
		@SuppressWarnings("unchecked")
		List<TComments> list=(List<TComments>) commentService.getCommentsById(pager, contentId,type,lastTime,userId);
		data.put("CommentList", list);
		data.put("CommentAmount", commentService.getCommentsAmount(contentId, type));
		data.put("Page", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);
		return JSONUtil.toJSON(result);
	}
	
	
	/**
	 * PC查询所有评论
	 * 
	 * @param pager
	 * @param contentId
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getAllCommentsList")
	public Object getAllCommentsById(String contentId,String type,String userId) throws Exception {

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		
		Map<String, Object> data = new HashMap<String, Object>();
		@SuppressWarnings("unchecked")
		List<TComments> list=(List<TComments>) commentService.getAllCommentsById(contentId,type,userId);
		data.put("CommentList", list);
		data.put("CommentAmount", commentService.getAllCommentsAmount(contentId, type));
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * Block评论
	 * @param commentId
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveBlockComment")
	public Object saveBlockComments(String commentId,String userId) throws Exception {
		String[] filter = new String[] {};
		
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}

		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);

		data.put("commentObj", commentService.saveBlockComments(commentId,userId));
		return JSONUtil.toJSON(result, filter);
	}
	
	/**
	 * show评论
	 * @param commentId
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveShowComments")
	public Object saveShowComments(String commentId,String userId) throws Exception {
		String[] filter = new String[] {};
		
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}

		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);

		data.put("commentObj", commentService.saveShowComments(commentId, userId));
		return JSONUtil.toJSON(result, filter);
	}
}
